package xin.printk.android.module.plan;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import xin.printk.android.BasicActivity;
import xin.printk.android.R;
import xin.printk.android.data.entity.Plan;
import xin.printk.android.utils.DateUtils;
import xin.printk.android.utils.LogUtils;

public class AddPlanActivity extends BasicActivity implements AddPlanContract.View {

    private EditText mTitleEditText;
    private EditText mContentEditText;
    private Button mTimeButton;
    private Button mLevelButton;

    private int level = 2;

    private long selectTime = 0;

    private int mYear;
    private int mMonth;
    private int mDayOfMonth;
    private int mHourOfDay;
    private int mMinute;
    private String mDate;

    private AddPlanContract.Presenter mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_plan);
        Toolbar toolbar = findViewById(R.id.new_plan_toolbar);
        setToolbar(toolbar);
        setCanFinish(true);
        mPresenter = new AddPlanPresenter(this);
        mTitleEditText = findViewById(R.id.et_plan_title);
        mContentEditText = findViewById(R.id.et_plan_content);
        mTimeButton = findViewById(R.id.bt_plan_time);
        mLevelButton = findViewById(R.id.bt_level);

        mTimeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDataDialog();
            }
        });

        mLevelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSingleDialog();
            }
        });

        FloatingActionButton fab_done = findViewById(R.id.fab_done);
        fab_done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.addPlan();
            }
        });

        Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        mHourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
        mMinute = calendar.get(Calendar.MINUTE);
        mDate = mYear + "-" + (mMonth + 1) + "-" + mDayOfMonth + " " + mHourOfDay + ":" + mMinute;
        LogUtils.d(mDate);
        mTimeButton.setText(mDate);
        try {
            selectTime = DateUtils.dateToTimeStamp(mDate);
        } catch (ParseException e) {
            LogUtils.e(e.getMessage());
            selectTime = 0;
        }
    }

    private void showDataDialog() {
        final DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                mYear = year;
                mMonth = month;
                mDayOfMonth = dayOfMonth;
                showTimePicker();
            }
        }, mYear, mMonth, mDayOfMonth);
        dialog.show();
    }

    private void showTimePicker() {
        TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHourOfDay = hourOfDay;
                mMinute = minute;
                mDate = mYear + "-" + (mMonth + 1) + "-" + mDayOfMonth + " " + mHourOfDay + ":" + mMinute;
                LogUtils.d(mDate);
                mTimeButton.setText(mDate);
                try {
                    selectTime = DateUtils.dateToTimeStamp(mDate);
                } catch (ParseException e) {
                    LogUtils.e(e.getMessage());
                    selectTime = 0;
                }
            }
        }, mHourOfDay, mMinute, true);
        dialog.show();
    }

    private void showSingleDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.plan_select_level);
        builder.setSingleChoiceItems(mLevelItems, level, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                level = which;
                mLevelButton.setText(mLevelItems[which]);
                mLevelButton.setTextColor(getResources().getColor(mLevelColorResId[which]));
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    @Override
    public Context getContext() {
        return getApplicationContext();
    }

    @Override
    public Plan getPlan() {
        Plan plan = new Plan();
        plan.setUserId(0);
        plan.setTitle(mTitleEditText.getText().toString());
        plan.setContent(mContentEditText.getText().toString());
        plan.setStatus(0);
        plan.setLevel(level);
        plan.setCreateTime(DateUtils.dateToTimeStamp(new Date()));
        plan.setPlanTime(selectTime);
        return plan;
    }

    @Override
    public void add() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void showError(String message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
    }
}
